package chapter10;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/4/13 15:06
 */
interface Counter {
    int next();
}

public class LocalInnerClass {
    private int counter = 0;

    public static void main(String[] args) {
        LocalInnerClass lic = new LocalInnerClass();
        Counter c1 = lic.getCounter("Local inner");
        Counter c2 = lic.getCounter2("Anonymous inner");
        for (int i = 0; i < 5; i++) {
            System.out.println(c1.next());
        }

        for (int i = 0; i < 5; i++) {
            System.out.println(c2.next());
        }
    }

    Counter getCounter(final String name) {
        // a local inner class
        class LocalCounter implements Counter {
            public LocalCounter() {
                System.out.println("LocalCounter");
            }

            @Override
            public int next() {
                System.out.print(name);
                return counter++;
            }
        }

        return new LocalCounter();
    }

    Counter getCounter2(final String name) {
        return new Counter() {
            {
                System.out.println("Counter");
            }

            @Override
            public int next() {
                System.out.print(name);
                return counter++;
            }
        };
    }
}
